#!/usr/bin/python 
# -*- coding: UTF-8 -*-
import os
cur_dir = os.path.dirname(os.path.abspath(__file__))

from geraldo.base import *
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm,mm
from reportlab.lib.fonts import *
from reportlab.lib.enums import TA_CENTER,TA_RIGHT
from reportlab.lib.colors import red,pink,navy
from django.utils.encoding import smart_str,smart_unicode
from wikicamp.worklist.models import *
from geraldo import *

class islistesiRapor(Report):
    title = u"İş Listesi"
    author = 'Oesh Bim'

    page_size = landscape(A4)
    margin_left = 2*cm
    margin_top = 1*cm
    margin_right = 0.5*cm
    margin_bottom = 1*cm
    
    class band_begin(ReportBand):
        height = 0.5*cm
        elements = [
            Label(text='', top=0.1*cm,
                left=8*cm),
        ]
    
    class band_detail(ReportBand):
        auto_expand_height = True
        display_inline = True
        height = 0.5*cm
        margin_bottom = 0.2*cm
      
        elements=(
            ObjectValue(attribute_name='timestamp', left=0*cm,width=2*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
            ObjectValue(attribute_name='kullanici', left=2.1*cm,width=2.5*cm,get_value=lambda instance: u"%s".encode('UTF-8').lower().capitalize() %  instance.kullanici.get_full_name(),style={'fontName': 'sanz', 'fontSize': 8} ),
            ObjectValue(attribute_name='atolye', left=4.8*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.atolye.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='lokasyon', left=7*cm,width=2.3*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.lokasyon.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='turu', left=9.5*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.turu.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='islem_durum', left=11.7*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.islem_durum.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='aciklama', left=14*cm,width=5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='eknot', left=19.3*cm,width=5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.eknot,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='onaylayan_kullanici', left=25*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  checkifnone(instance.onaylayan_kullanici),style={'fontName': 'sanz', 'fontSize': 8})
            
            
            #ObjectValue(attribute_name='kullanici', left=3*cm,get_value=lambda instance: u"descr: %s" %  removeturkish(instance.aciklama) )
            )
        
        
        
    class band_page_header(ReportBand):
        height = 1*cm
        margin_bottom = 1*cm
        elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'sanzbold', 'fontSize': 10, 'alignment': TA_CENTER}),
                Label(text="Tarih", top=0.5*cm, left=0*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Isteyen", top=0.5*cm, left=2.1*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Atolye", top=0.5*cm, left=4.8*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Lokasyon", top=0.5*cm, left=7*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Türü", top=0.5*cm, left=9.5*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Durumu", top=0.5*cm, left=11.7*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Açıklama", top=0.5*cm, left=14*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Süreç Notu", top=0.5*cm, left=19*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"İş Sahibi", top=0.5*cm, left=25*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                SystemField(expression=u'Sayfa %(page_number)d / %(page_count)d', top=0.1*cm,
                    width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                ]
        borders = {'bottom':  Line(stroke_color=red, stroke_width=0.5)}

    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
                Label(text='Geraldo Reports', top=0.1*cm,style={'fontName': 'sanz', 'fontSize': 8,'alignment': TA_RIGHT}),
                SystemField(expression=u'Olusturma %(now:%Y, %b %d)s at %(now:%H:%M)s', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontName': 'sanz', 'fontSize': 7,'alignment': TA_RIGHT}),
                ]
        borders = {'top': True}
        
    class band_summary(ReportBand):
        height = 1*cm
        elements = [
            Label(text=u"Toplam Is : ", top=0.1*cm, left=0.2*cm),
            ObjectValue(attribute_name='id', top=0.1*cm, left=4*cm,\
                action=FIELD_ACTION_COUNT, display_format='%s is listelendi.'),

            Label(text=u"Onaylanmislar : ", top=0.5*cm, left=0.2*cm),
            ObjectValue(attribute_name='onaylandi', top=0.5*cm, left=4*cm, action=FIELD_ACTION_SUM),

        ]
        borders = {'all': RoundRect(radius=5, fill_color=pink, fill=True)}

class bakimlistesiRapor(Report):
    title = 'Planlanmis Bakim - Kalibrasyonlar '
    author = 'Oesh Bim'

    page_size = landscape(A4)
    margin_left = 2*cm
    margin_top = 1.5*cm
    margin_right = 0.5*cm
    margin_bottom = 1*cm

    class band_begin(ReportBand):
        height = 0.5*cm
        elements = [
            Label(text='', top=0.1*cm,
                left=8*cm),
        ]

    class band_detail(ReportBand):
        auto_expand_height = True
        display_inline = True
        height = 0.5*cm
        margin_bottom = 0.5*cm
        
        elements=(
            ObjectValue(attribute_name='aciklama', left=0*cm,width=8*cm,get_value=lambda instance: u"%s".encode('UTF-8').lower().capitalize() %  instance.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='atolye', left=8.5*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.atolye.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='turu', left=11*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.turu.aciklama,style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='donem', left=13.5*cm,width=6*cm,get_value=lambda instance: u"%s".lower().capitalize() %  str(instance.planlanan_baslangic_tarihi.date())+" ile "+str(instance.planlanan_bitis_tarihi.date()) + " arasinda",style={'fontName': 'sanz', 'fontSize': 8}),
            ObjectValue(attribute_name='durum', left=19*cm,width=7*cm,get_value=lambda instance: u"%s".lower().capitalize() %  bakildi(instance.id),style={'fontName': 'sanz', 'fontSize': 8})
            
            
            
            #ObjectValue(attribute_name='kullanici', left=3*cm,get_value=lambda instance: u"descr: %s" %  removeturkish(instance.aciklama) )
            )
        
        
        
    class band_page_header(ReportBand):
        height = 1.5*cm
        margin_bottom = 1*cm
        margin_top = 2*cm
        
        elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'Helvetica-Bold', 'fontSize': 10, 'alignment': TA_CENTER}),
                Label(text=u"Aciklama", top=0.8*cm, left=0*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Atolye", top=0.8*cm, left=8.5*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Türü", top=0.8*cm, left=11*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Dönemi", top=0.8*cm, left=13.5*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                Label(text=u"Durum", top=0.8*cm, left=19*cm,style={'fontName': 'sanzbold', 'fontSize': 8}),
                SystemField(expression=u'Sayfa %(page_number)d / %(page_count)d', top=0.1*cm,
                    width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                ]
        borders = {'bottom':  Line(stroke_width=0.3)}

    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
                Label(text='Geraldo Reports', top=0.1*cm,style={'fontName': 'sanz', 'fontSize': 8,'alignment': TA_RIGHT}),
                SystemField(expression=u'Olusturma %(now:%Y, %b %d)s at %(now:%H:%M)s', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontName': 'sanz', 'fontSize': 7,'alignment': TA_RIGHT}),
                ]
        borders = {'top': True}
        
    class band_summary(ReportBand):
        height = 1*cm
        elements = [
            Label(text=u"Toplam Bakim: ", top=0.1*cm, left=0.2*cm),
            ObjectValue(attribute_name='id', top=0.1*cm, left=4*cm,\
                action=FIELD_ACTION_COUNT, display_format='%s bakim/kalibrasyon listelendi.'),

            #Label(text=u"Onaylanmislar : ", top=0.5*cm, left=0.2*cm),
            #ObjectValue(attribute_name='onaylandi', top=0.5*cm, left=4*cm, action=FIELD_ACTION_SUM),

        ]
        borders = {'all': RoundRect(radius=5, fill_color=pink, fill=True)}
        

class MasterReport(Report):
        
        
        
        def __init__(self, queryset, *args, **kwargs):
            self.queryset = queryset
            Report.__init__(self,queryset=queryset) 
            self.__class__.cihaz_id=self.queryset[0].id
            
        def subreports_olustur(self):
            subz = []
            sub_altcihazlar = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = cihazlar.objects.filter(parent_id= self.cihaz_id),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='CİHAZA BAĞLI PARÇALAR', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                        #borders={'top': True},
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        elements=[
                           ObjectValue(attribute_name='aciklama', top=0, left=0.2*cm,width=BAND_WIDTH,get_value=lambda instance: '&#x2022;   ' + u"%s" % instance.aciklama.lower().capitalize() + " [" + instance.demirbasno +" ]",style={'fontName': 'sanz','fontSize': 9,'alignment': TA_LEFT}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),  
            )
            
            sub_altcihazbos = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = User.objects.filter(id=1),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='CİHAZA BAĞLI PARÇALAR', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                        #borders={'top': True},
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        elements=[
                           Label(text=u"---- Cihaza Bağlı herhangi bir ek cihaz bulunamadı.. ----", top=0, left=0.2*cm,width=BAND_WIDTH,style={'fontName': 'sanz','fontSize': 9,'alignment': TA_CENTER}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),
            )
            
            sub_arizalistesi = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = islistesi.objects.filter(cihaz= cihazlar.objects.get(id=self.cihaz_id)).exclude(turu__in=isturu.objects.filter(id__in=[4,6])),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='ARIZA TAKİP ÇİZELGESİ', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                       
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            ObjectValue(attribute_name='turu', top=0, left=0.2*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.turu.aciklama,style={'fontName': 'sanz','fontSize': 8}),
                            ObjectValue(attribute_name='aciklama', top=0, left=2.2*cm,width=4.5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.aciklama,style={'fontName': 'sanz','fontSize': 8}),
                            ObjectValue(attribute_name='atolye', top=0, left=7*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.atolye.aciklama,style={'fontName': 'sanz','fontSize': 8}),
                            ObjectValue(attribute_name='kullanici', top=0, left=9.1*cm,width=2.4*cm,get_value=lambda instance: u"%s".encode('UTF-8').lower().capitalize() %  instance.kullanici.get_full_name(),style={'fontName': 'sanz','fontSize': 8}),
                            ObjectValue(attribute_name='timestamp', left=11.7*cm,width=1.5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.timestamp,style={'fontName': 'sanz', 'fontSize': 8}),
                            ObjectValue(attribute_name='eknot', left=13.5*cm,width=4.8*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.eknot,style={'fontName': 'sanz', 'fontSize': 8}),
                            ObjectValue(attribute_name='durum', left=18.7*cm,width=1*cm,get_value=lambda instance: '%s' %  onaylandi(instance.id),style={'fontName': 'sanz', 'fontSize': 8}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),
            )
            
            sub_arizabos = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = User.objects.filter(id=1),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='ARIZA TAKİP ÇİZELGESİ', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                       
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            Label(text=u"---- Cihaza ait herhangi bir arıza kaydı bulunamadı.. ----", top=0, left=0.2*cm,width=BAND_WIDTH,style={'fontName': 'sanz','fontSize': 9,'alignment': TA_CENTER}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),
            )
            
            sub_bakimlar = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = islistesi.objects.filter(cihaz= cihazlar.objects.get(id=self.cihaz_id),turu=isturu.objects.filter(id__in=[4])),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='BAKIM TAKİP ÇİZELGESİ', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                       
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            ObjectValue(attribute_name='turu', top=0, left=0.2*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.turu.aciklama,style={'fontName': 'sanz','fontSize': 9}),
                            ObjectValue(attribute_name='aciklama', top=0, left=2.2*cm,width=5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.aciklama,style={'fontName': 'sanz','fontSize': 9}),
                            ObjectValue(attribute_name='atolye', top=0, left=7.5*cm,width=2.5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.atolye.aciklama,style={'fontName': 'sanz','fontSize': 9}),
                            ObjectValue(attribute_name='timestamp', left=10.5*cm,width=8*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.planlanan_baslangic_tarihi.strftime("%d.%m.%Y") + " - " + instance.planlanan_bitis_tarihi.strftime("%d.%m.%Y") + u" tarihleri arasında",style={'fontName': 'sanz', 'fontSize': 9}),
                            ObjectValue(attribute_name='durum', left=18.7*cm,width=1*cm,get_value=lambda instance: '%s' %  getbakimtablo(instance.id),style={'fontName': 'sanz', 'fontSize': 9}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),
            )
            
            sub_bakimbos = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = User.objects.filter(id=1),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='BAKIM TAKİP ÇİZELGESİ', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                       
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            Label(text=u"---- Cihaza ait herhangi bir bakım kaydı bulunamadı.. ----", top=0, left=0.2*cm,width=BAND_WIDTH,style={'fontName': 'sanz','fontSize': 9,'alignment': TA_CENTER}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),
            )
            
            sub_kalibrasyonlar = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = islistesi.objects.filter(cihaz= cihazlar.objects.get(id=self.cihaz_id),turu=isturu.objects.filter(id__in=[6])),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='KALİBRASYON TAKİP ÇİZELGESİ', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                       
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            ObjectValue(attribute_name='turu', top=0, left=0.2*cm,width=2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.turu.aciklama,style={'fontName': 'sanz','fontSize': 9}),
                            ObjectValue(attribute_name='aciklama', top=0, left=2.2*cm,width=5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.aciklama,style={'fontName': 'sanz','fontSize': 9}),
                            ObjectValue(attribute_name='atolye', top=0, left=7.5*cm,width=2.5*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.atolye.aciklama,style={'fontName': 'sanz','fontSize': 9}),
                            ObjectValue(attribute_name='timestamp', left=10.5*cm,width=8*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.planlanan_baslangic_tarihi.strftime("%d.%m.%Y") + " - " + instance.planlanan_bitis_tarihi.strftime("%d.%m.%Y") + u" tarihleri arasında",style={'fontName': 'sanz', 'fontSize': 9}),
                            ObjectValue(attribute_name='durum', left=18.7*cm,width=1*cm,get_value=lambda instance: '%s' %  getbakimtablo(instance.id),style={'fontName': 'sanz', 'fontSize': 9}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),
            )
            
            sub_kalibrasyonbos = SubReport(
                #queryset_string = '%(object)s.objects.filter(parent_id=int(%(object)s.id))',
                #queryset_string = 'atolyeler.objects.all()',
                queryset_def = User.objects.filter(id=1),
                band_header = ReportBand(
                        height=0.5*cm,
                        margin_top = 1*cm,
                        elements=[
                            Label(text='KALİBRASYON TAKİP ÇİZELGESİ', top=1*cm, left=0*cm,width=BAND_WIDTH, style={'fontName': 'sanzbold','alignment': TA_CENTER}),
                            
                        ],
                       
                    ),
                band_detail = ReportBand(
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            Label(text=u"---- Cihaza ait herhangi bir kalibrasyon kaydı bulunamadı.. ----", top=0, left=0.2*cm,width=BAND_WIDTH,style={'fontName': 'sanz','fontSize': 9,'alignment': TA_CENTER}),
                        ],
                        
                    ),
                band_footer = ReportBand(
                        height=0.1*cm,
                        elements=[
                            #ObjectValue(attribute_name='id', left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s permissions found',style={'fontName': 'sanzbold'}),
                        ],
                        borders={'bottom': True,},
                    ),
            )
            
            try: 
                xsay=cihazlar.objects.filter(parent_id= self.cihaz_id).count()
                
                if xsay > 0 :
                    subz.append(sub_altcihazlar)
                else:
                    subz.append(sub_altcihazbos)
            except:
                    subz.append(sub_altcihazbos)
            
            try:  
                xsay= islistesi.objects.filter(cihaz= cihazlar.objects.get(id=self.cihaz_id)).exclude(turu__in=isturu.objects.filter(id__in=[4,6])).count()
                
                if xsay > 0 :
                    subz.append(sub_arizalistesi)
                else:
                    subz.append(sub_arizabos)
            except:
                    subz.append(sub_arizabos)
            
            try:  
                xsay= islistesi.objects.filter(cihaz= cihazlar.objects.get(id=self.cihaz_id),turu=isturu.objects.filter(id__in=[4])).count()
                
                if xsay > 0 :
                    subz.append(sub_bakimlar)
                else:
                    subz.append(sub_bakimbos)
            except:
                    subz.append(sub_bakimbos)
            
            try:  
                xsay= islistesi.objects.filter(cihaz= cihazlar.objects.get(id=self.cihaz_id),turu=isturu.objects.filter(id__in=[6])).count()
                
                if xsay > 0 :
                    subz.append(sub_kalibrasyonlar)
                else:
                    subz.append(sub_kalibrasyonbos)
            except:
                    subz.append(sub_kalibrasyonbos)
            
            return subz
        
        
            
        title = u"Ege Sağlık Hastanesi Cihaz Kartı"
         
        class band_summary(ReportBand):
            height = 0.8*cm
            elements = [
                #Label(text="Users count:", top=0.1*cm, left=0),
                #ObjectValue(attribute_name='id', top=0.1*cm, left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s users found'),
            ]
            borders = {'top': Line(stroke_color=red, stroke_width=1)}
   
        class band_page_header(ReportBand):
            height = 0.8*cm
            elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'sanzbold', 'fontSize': 14, 'alignment': TA_CENTER}),
                SystemField(expression='Sayfa # %(page_number)d / %(page_count)d', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontName': 'sanz','fontSize':8,'alignment': TA_RIGHT}),
            ]
            borders = {'bottom': Line(stroke_color=red, stroke_width=3)}
   
        class band_page_footer(ReportBand):
            height = 0.5*cm
            elements = [
                Label(text="Geraldo Reports ile oluşturuldu", top=0.1*cm,style={'fontName': 'sanz','fontSize':8}),
                SystemField(expression='Basılma tarihi %(now:%Y, %b %d)s at %(now:%H:%M)s', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontName': 'sanz','fontSize':8,'alignment': TA_RIGHT}),
            ]
            borders = {'top': Line(stroke_color=navy)}
   
        class band_detail(ReportBand):
            margin_top=0.5*cm
            auto_expand_height = True
            height = 4*cm
            elements = [
                Label(text=u"Cihaz Adı", top=0.5*cm, left=0.2*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                Label(text=u"Markası", top=1*cm, left=0.2*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                Label(text=u"Modeli", top=1.5*cm, left=0.2*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                Label(text=u"Seri No", top=2*cm, left=0.2*cm, style={'fontName': 'sanzbold' ,'fontSize': 9}),
                Label(text=u"Aidiyeti", top=2.5*cm, left=0.2*cm, style={'fontName': 'sanzbold' ,'fontSize': 9}),
                Label(text=u"Garanti Süresi", top=3*cm, left=0.2*cm, style={'fontName': 'sanzbold' ,'fontSize': 9}),
                Label(text=u"Bulunduğu Yer", top=1*cm, left=11*cm, style={'fontName': 'sanzbold' ,'fontSize': 9}),
                Label(text=u"Yetkili Servisi", top=1.5*cm, left=11*cm, style={'fontName': 'sanzbold' ,'fontSize': 9}),
                Label(text=u"Barkod No", top=2*cm, left=11*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                Label(text=u"Demirbaş No", top=2.5*cm, left=11*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                ObjectValue(attribute_name='aciklama', top=0.5*cm, left=4*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='markasi', top=1*cm, left=4*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='modeli', top=1.5*cm, left=4*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='aidiyet', top=2.5*cm, left=4*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='garanti_bitis_tarihi', top=3*cm, left=4*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='lokasyon', top=1*cm, left=14*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='yetkili_servisi', top=1.5*cm, left=14*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='barkod', top=2*cm, left=14*cm, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='demirbasno', top=2.5*cm, left=14*cm, style={'fontName': 'sanz','fontSize': 9}),
            ]
            borders={'bottom': True,}

class bakimtablosuRapor(Report):
        
        title = u"Ege Sağlık Hastanesi Cihaz bakım formu"
        #additional_fonts = {'sanz': os.path.join(cur_dir, '/usr/share/fonts/truetype/freefont/FreeSans.ttf'),'sanzbold': os.path.join(cur_dir, '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf'),}
        #default_style = {'fontName': 'sanz'}

        def __init__(self, queryset, *args, **kwargs):
            self.queryset = queryset
            Report.__init__(self,queryset=queryset) 
            self.__class__.tablo_id=self.queryset[0].id
         
        def subreports_olustur(self):
            subz = []
            sub_bakimnoktalari = SubReport(
                #queryset_string = '%(object)s.taslak.kayit.filter()',
                queryset_def = bakimtablosu.objects.get(id=self.tablo_id).taslak.kayit.filter(),
                band_header = ReportBand(
                        height=0.7*cm,
                        margin_bottom = 0.2*cm,
                        elements=[
                            Label(text=u"Bakım Noktaları", top=0, left=0.2*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                            Label(text=u"Durum", top=0, left=8*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                            Label(text=u"Ek Not (varsa)", top=0, left=13*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                        ],
                        borders={'bottom': True},
                    ),
                band_detail = ReportBand(
                        height=0.5*cm,
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            ObjectValue(attribute_name='aciklama', top=0, left=0.2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.bakimnoktasi.aciklama,style={'fontName': 'sanz','fontSize': 9}),
                            ObjectValue(attribute_name='durum', top=0, left=8*cm,get_value=lambda instance: u"%s" %  get_durum(instance.durum), style={'fontName': 'sanzbold','fontSize': 9}),
                            ObjectValue(attribute_name='aciklama', top=0, left=13*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.eknot,style={'fontName': 'sanz','fontSize': 9}),
                            
                        ],
                        
                    ),
                    
            )
            subz.append(sub_bakimnoktalari)
            return subz
        
        class band_summary(ReportBand):
            height = 0.8*cm
            elements = [
                #Label(text="Users count:", top=0.1*cm, left=0),
                #ObjectValue(attribute_name='id', top=0.1*cm, left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s users found'),
            ]
            borders = {'top': Line(stroke_color=red, stroke_width=1)}
   
        class band_page_header(ReportBand):
            height = 0.8*cm
            elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'sanzbold', 'fontSize': 14, 'alignment': TA_CENTER}),
                SystemField(expression='Sayfa # %(page_number)d / %(page_count)d', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontName': 'sanz','fontSize':8,'alignment': TA_RIGHT}),
            ]
            borders = {'bottom': Line(stroke_color=red, stroke_width=3)}
   
        class band_page_footer(ReportBand):
            height = 0.5*cm
            elements = [
                Label(text="Geraldo Reports ile oluşturuldu", top=0.1*cm,style={'fontName': 'sanz','fontSize':8}),
                SystemField(expression='Basılma tarihi %(now:%Y, %b %d)s at %(now:%H:%M)s', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontName': 'sanz','fontSize':8,'alignment': TA_RIGHT}),
            ]
            borders = {'top': Line(stroke_color=navy)}
   
        class band_detail(ReportBand):
            margin_top=0.5*cm
            auto_expand_height = True
            height = 4*cm
            elements = [
                Label(text=u"Cihaz ve Açıklama ", top=0.5*cm, left=0.2*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                Label(text=u"Bağlı Olduğu Bakım", top=1*cm, left=0.2*cm, style={'fontName': 'sanzbold','fontSize': 9}),
                ObjectValue(attribute_name='aciklama', top=0.5*cm, left=4*cm,width=BAND_WIDTH, style={'fontName': 'sanz','fontSize': 9}),
                ObjectValue(attribute_name='islistesi', top=1*cm, left=4*cm, get_value= lambda instance: "%s" % instance.islistesi.aciklama.lower().capitalize() + ",&nbsp;Tarih:&nbsp;" + str(instance.islistesi.planlanan_baslangic_tarihi.month)+"/"+str(instance.islistesi.planlanan_baslangic_tarihi.year),width=BAND_WIDTH, style={'fontName': 'sanz','fontSize': 9}),
                
            ]
            #borders={'bottom': True,}

class bakimtablosucokluRapor(Report):
        
        title = u"Ege Sağlık Hastanesi Cihaz bakım formu"
        #additional_fonts = {'sanz': os.path.join(cur_dir, '/usr/share/fonts/truetype/freefont/FreeSans.ttf'),'sanzbold': os.path.join(cur_dir, '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf'),}
        #default_style = {'fontName': 'sanz'}

        def __init__(self, queryset, *args, **kwargs):
            self.queryset = queryset
            Report.__init__(self,queryset=queryset) 
            self.__class__.tablo_id=self.queryset[0].id
         
        def subreports_olustur(self):
            subz = []
            sub_bakimnoktalari = SubReport(
                #queryset_string = '%(object)s.taslak.kayit.filter()',
                queryset_def = bakimtablosu.objects.get(id=self.tablo_id).taslak.kayit.filter(),
                band_header = ReportBand(
                        height=0.7*cm,
                        margin_bottom = 0.2*cm,
                        elements=[
                            Label(text=u"Bakım Noktaları", top=0, left=0.2*cm, style={'fontSize': 9}),
                            Label(text=u"Durum", top=0, left=8*cm, style={'fontSize': 9}),
                            Label(text=u"Ek Not (varsa)", top=0, left=13*cm, style={'fontSize': 9}),
                        ],
                        borders={'bottom': True},
                    ),
                band_detail = ReportBand(
                        height=0.5*cm,
                        auto_expand_height = True,
                        display_inline = True,
                        elements=[
                            ObjectValue(attribute_name='aciklama', top=0, left=0.2*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.bakimnoktasi.aciklama,style={'fontSize': 9}),
                            ObjectValue(attribute_name='durum', top=0, left=8*cm,get_value=lambda instance: u"%s" %  get_durum(instance.durum), style={'fontSize': 9}),
                            ObjectValue(attribute_name='aciklama', top=0, left=13*cm,get_value=lambda instance: u"%s".lower().capitalize() %  instance.eknot,style={'fontSize': 9}),
                            
                        ],
                        
                    ),
                    
            )
            subz.append(sub_bakimnoktalari)
            return subz
        
        class band_summary(ReportBand):
            height = 0.8*cm
            elements = [
                #Label(text="Users count:", top=0.1*cm, left=0),
                #ObjectValue(attribute_name='id', top=0.1*cm, left=4*cm,action=FIELD_ACTION_COUNT, display_format='%s users found'),
            ]
            borders = {'top': Line(stroke_color=red, stroke_width=1)}
   
        class band_page_header(ReportBand):
            height = 0.8*cm
            elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={ 'fontSize': 14, 'alignment': TA_CENTER}),
                SystemField(expression='Sayfa # %(page_number)d / %(page_count)d', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontSize':8,'alignment': TA_RIGHT}),
            ]
            borders = {'bottom': Line(stroke_color=red, stroke_width=3)}
   
        class band_page_footer(ReportBand):
            height = 0.5*cm
            elements = [
                Label(text="Geraldo Reports ile oluşturuldu", top=0.1*cm,style={'fontSize':8}),
                SystemField(expression='Basılma tarihi %(now:%Y, %b %d)s at %(now:%H:%M)s', top=0.1*cm,
                    width=BAND_WIDTH, style={'fontSize':8,'alignment': TA_RIGHT}),
            ]
            borders = {'top': Line(stroke_color=navy)}
   
        class band_detail(ReportBand):
            margin_top=0.5*cm
            auto_expand_height = True
            height = 4*cm
            elements = [
                Label(text=u"Cihaz ve Açıklama ", top=0.5*cm, left=0.2*cm, style={'fontSize': 9}),
                Label(text=u"Bağlı Olduğu Bakım", top=1*cm, left=0.2*cm, style={'fontSize': 9}),
                ObjectValue(attribute_name='aciklama', top=0.5*cm, left=4*cm,width=BAND_WIDTH, style={'fontSize': 9}),
                ObjectValue(attribute_name='islistesi', top=1*cm, left=4*cm, get_value= lambda instance: "%s" % instance.islistesi.aciklama.lower().capitalize() + ",&nbsp;Tarih:&nbsp;" + str(instance.islistesi.planlanan_baslangic_tarihi.month)+"/"+str(instance.islistesi.planlanan_baslangic_tarihi.year),width=BAND_WIDTH, style={'fontSize': 9}),
                
            ]
            #borders={'bottom': True,}
        

def removeturkish(xstr):
    try:
        mystr= xstr.replace(u"İ",u"i").replace(u"Ş",u"S").replace(u"Ğ",u"G").replace(u"ı",u"I").replace(u"ğ",u"g").replace(u"ş",u"s")
        mystr=xstr
        mystr= mystr.lower().capitalize()
    except:
        mystr="--"
    
    return mystr

def checkifnone(xuser):
    try:
        xreturnuser= xuser.get_full_name()
    except Exception,e:
        xreturnuser = "--"
        
    return xreturnuser

def bakildi(xid):
    try:
        xsay = bakimtablosu.objects.filter(islistesi=islistesi.objects.get(id=xid)).count()
        if xsay > 0 :
            
            return u"Bakim gerçeklesmiş, formu doldurulmuş"
        
        else:
            
            return u"Bakım yapılmamış, form yok"
    except:
        return False 

def onaylandi(xid):
    try:
        isistek_ = islistesi.objects.get(id=xid)
        if not isistek_.onaylandi:
            
            return "-"
        
        else:
            
            return "+"
    except:
        return "?"

def getbakimtablo(xid):
    try:
        xsay = bakimtablosu.objects.filter(islistesi=islistesi.objects.get(id=xid)).count()
        if xsay > 0 :
            return "+"
        else:
            return "-"
    except:
        return "?"

def get_durum(x):
    try:
        if x==True:
            return u"GEÇTİ"
        else:
            return u"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KALDI"
    except:
        return "~HATA~"

